// 数据结构接口
interface DataStructure {
    void accept(Visitor visitor);
}

// 具体数据结构类 - 树结构
class Tree implements DataStructure {
    private TreeNode root;

    public Tree(TreeNode root) {
        this.root = root;
    }

    public TreeNode getRoot() {
        return root;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

// 树节点类
class TreeNode {
    private int value;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }
}

// 具体数据结构类 - 图结构
class Graph implements DataStructure {
    private List<Node> nodes;

    public Graph(List<Node> nodes) {
        this.nodes = nodes;
    }

    public List<Node> getNodes() {
        return nodes;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

// 图节点类
class Node {
    private int id;
    private List<Node> neighbors;

    public Node(int id) {
        this.id = id;
        this.neighbors = new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public List<Node> getNeighbors() {
        return neighbors;
    }

    public void addNeighbor(Node neighbor) {
        neighbors.add(neighbor);
    }
}

// 算法接口
interface Visitor {
    void visit(Tree tree);

    void visit(Graph graph);
}

// 具体算法类
class ConcreteVisitor implements Visitor {
    @Override
    public void visit(Tree tree) {
        System.out.println("Applying algorithm to tree structure...");
        // 实现对树结构的算法操作
        // 这里可以添加一些算法逻辑
    }

    @Override
    public void visit(Graph graph) {
        System.out.println("Applying algorithm to graph structure...");
        // 实现对图结构的算法操作
        // 这里可以添加一些算法逻辑
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 创建具体的树结构
        TreeNode root = new TreeNode(1);
        TreeNode leftChild = new TreeNode(2);
        TreeNode rightChild = new TreeNode(3);
        root.setLeft(leftChild);
        root.setRight(rightChild);
        Tree tree = new Tree(root);

        // 创建具体的图结构
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.addNeighbor(node2);
        node2.addNeighbor(node3);
        List<Node> nodes = new ArrayList<>();
        nodes.add(node1);
        nodes.add(node2);
        nodes.add(node3);
        Graph graph = new Graph(nodes);

        // 创建具体的算法对象
        ConcreteVisitor visitor = new ConcreteVisitor();

        // 树结构对象接受算法对象的访问
        tree.accept(visitor);

        // 图结构对象接受算法对象的访问
        graph.accept(visitor);
    }
}
